<?php
/**
 * File containing the BRail_Teamware_Adapter_Factory class.
 * @package BRail_Teamware
 */

/**
 * @package BRail_Teamware
 */
class BRail_Teamware_Adapter_Factory
{
    const CONFIG_FILE = 'environment.php';

    /**
     * @return BRail_Teamware_Adapter
     */
    public static function getAdapter()
    {
        static $twAdapter;
        if (!$twAdapter instanceof BRail_Teamware_Adapter) {
            $twAdapter = new BRail_Teamware_Adapter(self::getConfig());
        }
        return $twAdapter;
    }

    /**
     * @return array
     * @throws BRail_Teamware_Exception
     */
    private static function getConfig()
    {
        static $twConfig;
        if (! is_array($twConfig)) {
            $configFull = require dirname(dirname(realpath(__FILE__))) . DIRECTORY_SEPARATOR . 'config' .
                            DIRECTORY_SEPARATOR . self::CONFIG_FILE;
            $env = getenv('ENVPHP');
            if (!isset($configFull[$env])) {
                throw new BRail_Teamware_Exception(__METHOD__.' requires a valid ENVPHP.');
            }
            $twConfig = $configFull[$env];
        }
        return $twConfig;
    }
}